//
//  TravelTableViewCell.m
//  demo
//
//  Created by dllo on 15/7/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "TravelTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"

@implementation TravelTableViewCell
- (void)dealloc
{
    [_view release];
    [_img release];
    [_label1 release];
    [_label2 release];
    [_btn release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.view = [[UIView alloc] init];
        self.view.clipsToBounds = YES;
        [self.contentView addSubview:self.view];
        [_view release];
        
        self.img = [[UIImageView alloc] init];
        [self.view addSubview:self.img];
        [_img release];
        
        self.backImg = [[UIImageView alloc] init];
        [self.view addSubview:self.backImg];
        [_backImg release];
        
        self.shimmeringView = [[FBShimmeringView alloc] init];
        self.shimmeringView.shimmering = YES;
        self.shimmeringView.shimmeringBeginFadeDuration = 0.5;
        self.shimmeringView.shimmeringOpacity = 0.5;
        [self.backImg addSubview:self.shimmeringView];
        
        self.label1 = [[UILabel alloc] init];
        self.label1.textColor = [UIColor whiteColor];
        [self.label1 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        self.shimmeringView.contentView = self.label1;
        [_label1 release];
    
        
        self.label2 = [[UILabel alloc] init];
        self.label2.textColor = [UIColor whiteColor];
        [self.label2 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        [self.backImg addSubview:self.label2];
        [_label2 release];
        
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.layer.borderColor = [UIColor whiteColor].CGColor;
        self.btn.layer.borderWidth = 1;
        [self.btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.btn];
        [self setupImageWithTitle];
    }
    return self;
}
- (void)setupImageWithTitle
{
    self.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, ([UIScreen mainScreen].bounds.size.height - 49 - 64 - 40 - 10) / 2.5);
    self.img.frame = CGRectMake(10, -50, self.view.frame.size.width - 20, self.view.frame.size.height + 30);
    self.backImg.frame = CGRectMake(10, 0, self.view.bounds.size.width - 20, 65);
    self.shimmeringView.frame = CGRectMake(10, 10, self.backImg.bounds.size.width - 20, 25);
    self.label1.frame = CGRectMake(0, 0, self.backImg.bounds.size.width - 20, 25);
    self.label2.frame = CGRectMake(self.shimmeringView.frame.origin.x, self.shimmeringView.frame.origin.y + self.shimmeringView.frame.size.height + 2, self.img.bounds.size.width - 20, 25);
    self.btn.frame = CGRectMake(self.label2.frame.origin.x + 15, self.img.frame.origin.y + self.img.frame.size.height - 40, 40, 40);
    [self.btn.layer setCornerRadius:5.0];
    self.btn.layer.masksToBounds = YES;
}
- (void)btn:(UIButton *)btn
{
    
}
//- (void)layoutSubviews
//{
    //自定义cell中 重写了layout方法 一定要调用父类的layout方法(隔线乱现象)
//    [super layoutSubviews];
    
//    self.view.frame = CGRectMake(self.contentView.frame.origin.x + 10, self.contentView.frame.origin.y + 5, self.contentView.frame.size.width - 20, self.contentView.frame.size.height - 10);
//    
//    self.img.frame = CGRectMake(self.view.frame.origin.x - 10, self.view.frame.origin.y - 50, self.view.frame.size.width, self.view.frame.size.height + 100);
//    
//    self.label1.frame = CGRectMake(self.img.frame.origin.x + 10, self.img.frame.origin.y + 60, self.img.bounds.size.width - 20, 25);
//    
//    self.label2.frame = CGRectMake(self.label1.frame.origin.x, self.label1.frame.origin.y + self.label1.frame.size.height + 2, self.img.bounds.size.width - 20, 25);
//    
//    self.btn.frame = CGRectMake(self.label2.frame.origin.x, self.img.frame.origin.y + self.img.frame.size.height - 100, 40, 40);
//    [self.btn.layer setCornerRadius:20.0];
//    self.btn.layer.masksToBounds = YES;
//}
- (void)setTravel:(Travel *)travel
{
    if (_travel != travel) {
        [_travel release];
        _travel = [travel retain];
    }
    [self.img setImageWithURL:[NSURL URLWithString:travel.front_cover_photo_url] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
    self.label1.text = travel.name;
    self.label2.text = [NSString stringWithFormat:@"%@--%@/%d天%d图", travel.start_date, travel.end_date, travel.days.intValue, travel.photos_count.intValue];
    [self.btn setImageWithURL:[NSURL URLWithString:[travel.user objectForKey:@"image"]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
}
+ (instancetype)tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"tableViewCell";
    TravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[TravelTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    return cell;
}

- (void)cellOnTableView:(UITableView *)tableView didScrollOnView:(UIView *)view
{
    CGRect rectInSuperview = [tableView convertRect:self.frame toView:view];
    float distanceFromCenter = CGRectGetHeight(view.frame)/2 - CGRectGetMinY(rectInSuperview);
    float difference = CGRectGetHeight(self.img.frame) - CGRectGetHeight(self.frame);
    float move = (distanceFromCenter / CGRectGetHeight(view.frame)) * difference;
    CGRect imageRect = self.img.frame;
    imageRect.origin.y = -(difference/.5)+move;
    self.img.frame = imageRect;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
